#pragma once
#include<iostream>
#include<utility>
#include<cassert>
#include<cmath>
using namespace std;

template<class K, class V>
struct AVLTreeNode
{
	struct AVLTreeNode<K, V>* _left;
	struct AVLTreeNode<K, V>* _right;
	struct AVLTreeNode<K, V>* _parent;
	pair<K, V> _kv;
	int _bf;

	AVLTreeNode(const pair<K, V>& kv)
		:_left(nullptr), _right(nullptr), _parent(nullptr), _kv(kv), _bf(0)
	{}
};

template<class K, class V>
class AVLTree
{
public:
	typedef AVLTreeNode<K, V> Node;

	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}

		cur->_parent = parent;
		while (parent)
		{
			if (cur == parent->_left)
			{
				parent->_bf--;
			}
			else if (cur == parent->_right)
			{
				parent->_bf++;
			}
			else
			{
				assert(false);
			}


			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = cur->_parent;
				parent = parent->_parent;
			}
			else if(parent->_bf == 2 || parent->_bf == -2)
			{
				if (parent->_bf == 2 && cur->_bf == 1)
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)
				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					RotateLR(parent);
				}

				break;
			}
			else
			{
				assert(false);
			}
		}

		return true;
	}

	bool Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	bool IS_balance()
	{
		return IS_balance(_root);
	}

	void InOrder()
	{
		InOrder(_root);
	}

	void PostOrder()
	{
		PostOrder(_root);
	}

private:
	bool IS_balance(Node* root)
	{
		if (root == nullptr)
		{
			return true;
		}

		int LeftHeight = Height(root->_left);
		int RightHeight = Height(root->_right);
		if (abs(LeftHeight - RightHeight) > 1)
		{
			return false;
		}

		if (RightHeight - LeftHeight != root->_bf)
		{
			return false;
		}

		return IS_balance(root->_left)
			&& IS_balance(root->_right);
	}

	int Height(Node* root)
	{
		if (root == nullptr)
		{
			return 0;
		}
		
		return max(Height(root->_left), Height(root->_right)) + 1;
	}

	void RotateLR(Node* parent)
	{
		Node* subR = parent;
		Node* subL = parent->_left->_right;
		int bf = subL->_bf;

		RotateL(parent->_left);
		RotateR(parent);

		if (bf == -1)
		{
			subL->_bf = 0;
			subR->_bf = 1;
		}
		else if (bf == 1)
		{
			subL->_bf = -1;
			subR->_bf = 0;
		}
		else
		{
			subL->_bf = 0;
			subR->_bf = 0;
		}
	}

	void RotateRL(Node* parent)
	{
		Node* subL = parent;
		Node* subR = parent->_right->_left;
		int bf = subR->_bf;

		RotateR(parent->_right);
		RotateL(parent);

		if (bf == -1)
		{
			subL->_bf = 0;
			subR->_bf = 1;
		}
		else if (bf == 1)
		{
			subL->_bf = -1;
			subR->_bf = 0;
		}
		else
		{
			subL->_bf = 0;
			subR->_bf = 0;
		}
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if(subLR)
			subLR->_parent = parent;

		subL->_right = parent;

		Node* ppNode = parent->_parent;
		subL->_parent = ppNode;
		parent->_parent = subL;
		if (parent == _root)
		{
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
			if (parent == ppNode->_left)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
			}
		}
		
		subL->_bf = parent->_bf = 0;
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		subR->_left = parent;

		Node* ppNode = parent->_parent;
		subR->_parent = ppNode;
		parent->_parent = subR;
		if (parent == _root)
		{
			_root = subR;
			_root->_parent = nullptr;
		}
		else
		{
			if (parent == ppNode->_left)
			{
				ppNode->_left = subR;
			}
			else
			{
				ppNode->_right = subR;
			}
		}

		subR->_bf = parent->_bf = 0;
	}

	void InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		InOrder(root->_right);
	}

	void PostOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		InOrder(root->_left);
		InOrder(root->_right);
		cout << root->_key << " ";
	}

private:
	Node* _root = nullptr;
};
